/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.serli.sample.person.model.data;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.annotation.*;

/**
 *
 * @author serli
 */
//@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(name = "person", propOrder = {
    "id",
    "lastName",
    "firstName",
    "gender",
    "birthDateAsStr"
}, namespace = "data.model.person.sample.serli.com")
public class Person implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID =  8847687019439339999L;
    
    private int id;
    private String firstName = null;
    private String lastName = null;
    private Gender gender = Gender.MALE; 
    private Calendar birthDate = null;
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public Person() {
    }

    public Person(String firstName, String lastName, Calendar birthDate, Gender gender) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = birthDate;
        this.gender = gender;
    }
    
    public Person(String firstName, String lastName, String birthDate, Gender gender) throws ParseException {
        this.firstName = firstName;
        this.lastName = lastName;
        this.setBirthDateAsStr(birthDate);
        this.gender = gender;
    }

    public Person(int id, String firstName, String lastName, Calendar birthDate, Gender gender) {
        this(firstName, lastName, birthDate, gender);
        this.setId(id);
    }

    public int getAge() {

        Calendar bd = Calendar.getInstance();
        bd.setTime(getBirthDate().getTime());
        Calendar td = Calendar.getInstance();
        int age = td.get(Calendar.YEAR) - bd.get(Calendar.YEAR);
        if (td.get(Calendar.DAY_OF_YEAR) < bd.get(Calendar.DAY_OF_YEAR)) {
            age--;
        }
        return age;
    }

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @XmlTransient
    public Calendar getBirthDate() {
        return this.birthDate;
    }

    public void setBirthDate(Calendar birthDate) {
        this.birthDate = birthDate;
    }   
    
    public String getBirthDateAsStr() {
        String str = null;
        
        if (this.birthDate != null) {
            str =  dateFormat.format(this.birthDate.getTime());
        }
        else {
            str = "0000-00-00";
        }
        
        return str;
    }

    public final void setBirthDateAsStr(String birthDate) {
        try {
            Date d = dateFormat.parse(birthDate);
            this.birthDate = new GregorianCalendar();
            this.birthDate.setTime(d);
        } catch (ParseException ex) {
            Logger.getLogger(Person.class.getName()).log(Level.SEVERE, null, ex);
        }
    }   
 

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    public int getId() {
        return id;
    }

    final void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", firstName=" + firstName + ", lastName="
                + lastName + ", birthDate=" + birthDate + ", gender=" + gender
                + "]";
    }
}
